﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;
using System.IO;
using GVCSMTemplates;
using System.Collections;

namespace WebApplication1
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        public String imageSpeed;
        public String imageTimeout;
        public String imageTransitionEffect;
        public String textSpeed;
        public String textTimeout;
        public String textTransitionEffect;
        public String filmTitleFontColor;
        public String filmTitleFontSize;
        public String filmTitleFontType;
        public String filmRatingFontColor;
        public String filmRatingFontSize;
        public String filmRatingFontType;
        public String movieSessionTimingFontSize;
        public String movieSessionTimingFontColorAvailable;
        public String movieSessionTimingFontColorSellingFast;
        public String movieSessionTimingFontColorSoldOut;
        public String movieSessionTimingFontType;
        public String tableName;

        IniFile ini = new IniFile("c://Box/Signbox/settings.ini");
        private CalculateTableInformation ct;
        
       
 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadDefaultTransitionSettings();
                loadFilmInformationSettings();

                retrieveUICode();
                checkForAMPM();
                timerRefresh.Enabled = true;
            }
        }

 public void loadDefaultTransitionSettings()
 {
     tableName = ini.ReadValue("Table Information", "tableName");
     imageSpeed = ini.ReadValue("Normal Template", "imageSpeed");
     imageTimeout = ini.ReadValue("Normal Template", "imageTimeout");
     imageTransitionEffect = ini.ReadValue("Normal Template", "imageTransitionEffect");

     textSpeed = ini.ReadValue("Normal Template", "textSpeed");
     textTimeout = ini.ReadValue("Normal Template", "textTimeout");
     textTransitionEffect = ini.ReadValue("Normal Template", "textTransitionEffect");
 }

 public void loadFilmInformationSettings()
 {
     filmTitleFontColor = ini.ReadValue("Normal Template - Movie Information", "FilmTitleFontColor");
     filmTitleFontSize = ini.ReadValue("Normal Template - Movie Information", "FilmTitleFontSize");
     filmTitleFontType = ini.ReadValue("Normal Template - Movie Information", "FilmTitleFontType");
     filmRatingFontColor = ini.ReadValue("Normal Template - Movie Information", "FilmRatingFontColor");
     filmRatingFontSize = ini.ReadValue("Normal Template - Movie Information", "FilmRatingFontSize");
     filmRatingFontType = ini.ReadValue("Normal Template - Movie Information", "FilmRatingFontType");

     movieSessionTimingFontSize = ini.ReadValue("Normal Template - Movie Information", "MovieSessionTimingFontSize");
     movieSessionTimingFontColorAvailable = ini.ReadValue("Normal Template - Movie Information", "MovieSessionTimingFontColorAvailable");
     movieSessionTimingFontColorSellingFast = ini.ReadValue("Normal Template - Movie Information", "MovieSessionTimingFontColorSellingFast");
     movieSessionTimingFontColorSoldOut = ini.ReadValue("Normal Template - Movie Information", "MovieSessionTimingFontColorSoldOut");
     movieSessionTimingFontType = ini.ReadValue("Normal Template - Movie Information", "MovieSessionTimingFontType");
 }


        public void retrieveUICode()
        {
           

            string connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data source=C:\BOX\SIGNBOX\gv-box.mdb;Jet OLEDB:Database Password=";
            string sql2 = "select * from Sign_GVPlazaNormalTemplate";
            OleDbConnection conns = new OleDbConnection(connString);
            OleDbDataAdapter da = new OleDbDataAdapter();
            da.SelectCommand = new OleDbCommand(sql2, conns);

            DataSet ds = new DataSet();
            da.Fill(ds, "Sign_GVPlazaNormalTemplate");

            if (!(ds.Tables[0].Rows.Count <= 0))
            {
                DataTable dt = ds.Tables["Sign_GVPlazaNormalTemplate"];

               try
                {
                    String movieInformationHTML = displayMovieInformation(dt);
                    OutputMovieTitle.Text = movieInformationHTML + "</table></div></div>";
                    conns.Close();
                }

                catch (System.Exception ex)
                {
                    Response.Write("No Data" + ex.ToString());
                }
            }
            else
            {
                IniFile texttime = new IniFile(@"C:\BOX\SIGNBOX\testtime.ini");
                texttime.WriteValue("Sign_GVPlazaNormalTemplate", "time", DateTime.Now.ToString());
            }
        }

        public String displayMovieInformation(DataTable dt)
        {
            string strOutput = "";
            strOutput = "<div class='Textslideshow'>";

            int V1 = 0;
            string V2 = "";

            DataTable dtTwoRows = new DataTable();
            dtTwoRows = dt.Clone();
            dtTwoRows.Clear();

            ct = new GVCSMTemplates.CalculateTableInformation();

            dtTwoRows = ct.retrieveTableForNormalTemplate(dt);

            String posterInfo = displayImages(dtTwoRows);
            LiteralsImagesBK.Text = posterInfo;

            int test = dtTwoRows.Rows.Count;
            foreach (DataRow row in dtTwoRows.Rows)
            {
                string movieName = row["Film_Title"].ToString();

                if (V1 == 0)
                {
                    V2 = "<div><table width='100%'>";
                }
                else if (V1 % 1 == 0)
                {
                    V2 = "</table></div><div><table width='100%'>";
                }
                else
                {
                    V2 = "";
                }

                strOutput += V2 + "<tr>";

                strOutput += "<td colspan='3' width='2000px' align='left' valign='top'>";

                strOutput += "<B><font color='Black' style= \"font-family:" + filmTitleFontType + "; font-size:" + filmTitleFontSize + "; color:" + filmTitleFontColor + "; font-weight:bold; \">" + "  " + row["Film_Title"].ToString() + "</font></B></td></tr><tr><td height='5px'></td></tr>";
                strOutput += "<tr><td width='15%' align='LEFT' valign='bottom'> <font size='" + filmRatingFontSize + "' style= \"font-family:" + filmRatingFontType + ";\" color='" + filmRatingFontColor + "'>" + "  " + row["Rating"].ToString() + "</font></td></tr><tr><td height='10px'></td></tr>";

                //strOutput += "<tr><td class='RatingTemplateTextStyle' width='15%' align='left' valign='top'>" + row["Rating"].ToString() + "</td></tr><tr><td height='25px'></td></tr>";
                strOutput += "<tr><td width='100%' align='left' valign='left' style=\"font-family:@Futura Bk BT\"><table border=0 cellpadding=10 cellspacing=8><tr><td width='20%' align='left'>"

                + SessionStatus(row["Status1"].ToString(), row["Session_Time1"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status2"].ToString(), row["Session_Time2"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status3"].ToString(), row["Session_Time3"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status4"].ToString(), row["Session_Time4"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status5"].ToString(), row["Session_Time5"].ToString()) + "</td></tr><tr><td width='20%' align='left'>"


                + SessionStatus(row["Status6"].ToString(), row["Session_Time6"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status7"].ToString(), row["Session_Time7"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status8"].ToString(), row["Session_Time8"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status9"].ToString(), row["Session_Time9"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status10"].ToString(), row["Session_Time10"].ToString()) + "</td></tr><tr><td width='20%' align='left'>"


                + SessionStatus(row["Status11"].ToString(), row["Session_Time11"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status12"].ToString(), row["Session_Time12"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status13"].ToString(), row["Session_Time13"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status14"].ToString(), row["Session_Time14"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status15"].ToString(), row["Session_Time15"].ToString()) + "</td></tr><tr><td width='20%' align='left'>"

                + SessionStatus(row["Status16"].ToString(), row["Session_Time16"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status17"].ToString(), row["Session_Time17"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status18"].ToString(), row["Session_Time18"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status19"].ToString(), row["Session_Time19"].ToString()) + "</td><td width='20%' align='left'>"
                + SessionStatus(row["Status20"].ToString(), row["Session_Time20"].ToString()) + "</td><td width='20%' align='left'></td></tr></table>" + "</td>";

                strOutput += "</tr>";
                V1 = V1 + 1;
               
            }
            return strOutput;
        }

        public string additionalMovieInformation(DataRow row)
        {
            String additionalMovieTimingInfo = "";
            additionalMovieTimingInfo = "</table></div><div><table width='100%'>";
            additionalMovieTimingInfo += "<tr>";
            additionalMovieTimingInfo += "<td colspan='3' width='2000px' align='left' valign='top'>";
            additionalMovieTimingInfo += "<B><font color='White' style= \"font-family:Futura Bk BT; font-size:70px; color:white; font-weight:normal; \">" + "  " + row["Film_Title"].ToString() + "</font></B></td></tr><tr><td height='5px'></td></tr>";
            additionalMovieTimingInfo += "<tr><td width='15%' align='LEFT' valign='bottom'> <font size='5' font color='brown' font color='brown' style= \"font-family:Futura Bk BT;\" color:brown; >" + "  " + row["Rating"].ToString() + "</font></td></tr><tr><td height='10px'></td></tr>";
            additionalMovieTimingInfo += "<tr><td width='100%' align='left' valign='left' style=\"font-family:@Futura Bk BT\"><table border=0 cellpadding=10 cellspacing=8><tr><td width='20%' align='left'>"
            + SessionStatus(row["Status16"].ToString(), row["Session_Time16"].ToString()) + "</td><td width='20%' align='left'>"
            + SessionStatus(row["Status17"].ToString(), row["Session_Time17"].ToString()) + "</td><td width='20%' align='left'>"
            + SessionStatus(row["Status18"].ToString(), row["Session_Time18"].ToString()) + "</td><td width='20%' align='left'>"
            + SessionStatus(row["Status19"].ToString(), row["Session_Time19"].ToString()) + "</td><td width='20%' align='left'>"
            + SessionStatus(row["Status20"].ToString(), row["Session_Time20"].ToString()) + "</td><td width='20%' align='left'></td></tr></table>" + "</td>";
            additionalMovieTimingInfo += "</tr>";

            return additionalMovieTimingInfo;
        }


        public String displayImages(DataTable dt)
        {
            string posterImg = "";

            foreach (DataRow row in dt.Rows)
            {
                if (row["Media_Name"].ToString() != "" && row["Media_Path"].ToString() != "")
                {
                    posterImg = posterImg + "<img src='" + @"C:\BOX\SIGNBOX\MEDIA_PLAYER\Picture\img" + row["SHO_FILM_CD"] + ".jpeg'/>";
                }else
                {
                    String posterVal = posterTags(row);
                    posterImg = posterImg + posterVal.ToString();
                }     
                if(row["Session_Time16"].ToString() != "" && row["Status16"].ToString() != "")
                {
                    string displaySameMovieInfoImage = posterTags(row);
                    posterImg = posterImg + displaySameMovieInfoImage.ToString();
                }
            } return posterImg;
        }

        public string posterTags(DataRow row)
        {
            String posterImgHTMLTag = "";
            if (File.Exists(@"C:\BOX\SIGNBOX\MEDIA_PLAYER\Picture\img" + row["SHO_FILM_CD"].ToString() + ".jpg"))
            {
                posterImgHTMLTag = "<img src='" + @"http://localhost/images/img" + row["SHO_FILM_CD"] + ".jpg'/>";
            }
            else
            {
                posterImgHTMLTag = "<img src='" + @"http://localhost/images/" + "plazaDefault.jpg'/>";
            }
            return posterImgHTMLTag;
        }




        public string SessionStatus(string status, string time)
        {
            string strReturn = "";
            status = status.Replace(" ", "").ToUpper();
            if (status != "" && time != "")
            {
                switch (status)
                {
                    case "AVAILABLE":
                        strReturn = "<B><font color='" + movieSessionTimingFontColorAvailable + "' size='80px' style= \"font-family:" + movieSessionTimingFontType + "; font-size:" + movieSessionTimingFontSize + "; font-weight:bold;\" >" + time + "</font></B>";
                        break;
                    case "SELLINGFAST":
                        strReturn = "<B><font color='" + movieSessionTimingFontColorSellingFast + "' size='80px' style=\"font-family:" + movieSessionTimingFontType + "; font-size:" + movieSessionTimingFontSize + "; font-weight:bold;\" class=\"blk\" >" + time + "</font></B>";
                        break;
                    case "SOLDOUT":
                        strReturn = "<B><font color='" + movieSessionTimingFontColorSoldOut + "' size='80px' style=\"font-family:" + movieSessionTimingFontType + "; font-size:" + movieSessionTimingFontSize + "; font-weight:bold;\" >" + time + "</font></B>";
                        break;
                }
               
                return strReturn;
            }
            else
            {
                return strReturn;
            }
        }

        protected void timerRefresh_Tick(object sender, EventArgs e)
        {

            retrieveUICode();
        }

        public void checkForAMPM()
        {
            DateTime time = DateTime.Now;
            if (time.Hour > 12)
            {
                checkAMPM.Text = "PM";
            }
            else
            {
                checkAMPM.Text = "AM";
            }
        }




    }
}